module(..., package.seeall) -- 使得文件中的函数在何处都可调用

require "nvm"
require "io"
require "ntp"
require "logGenerator"
require "mqttTask"

-- 定义所需字段
local selfCheckContent, countSelfCheck, sysTime = "", 0, ""
-- 定时自检任务：系统状态、网络状态、服务连接状态等
local function timedSelfCheckTaske()
    local mqttConnectStat, netConnectStat, sysStat, netRssi, netMode,
          mqttConnectStatStr, netConnectStatStr, sysStatStr, netRssiStr,
          netModeStr
    sysTime = string.format("%04d-%02d-%02d %02d:%02d:%02d", os.date("*t").year,
                            os.date("*t").month, os.date("*t").day,
                            os.date("*t").hour, os.date("*t").min,
                            os.date("*t").sec)
    mqttConnectStat = mqttTask.isReady()
    netConnectStat = net.getState()
    netRssi = net.getRssi()
    netMode = net.getNetMode()
    if mqttConnectStat == true then
        mqttConnectStatStr = "成功"
    elseif mqttConnectStat == false then
        mqttConnectStatStr = "失败"
    end
    if netConnectStat == "INIT" then
        netConnectStatStr = "正在初始化"
    elseif netConnectStat == "REGISTERED" then
        netConnectStatStr = "已注册"
    elseif netConnectStat == "UNREGISTER" then
        netConnectStatStr = "未注册"
    end
    if netRssi >= 18 then
        netRssiStr = "一般"
    elseif netRssi >= 25 then
        netRssiStr = "良好"
    elseif netRssi < 18 then
        netRssiStr = "较差"
    end
    if netMode == 0 then
        netModeStr = "未注册"
    elseif netMode == 1 then
        netModeStr = "2G GSM网络"
    elseif netMode == 2 then
        netModeStr = "2.5G EDGE数据网络"
    elseif netMode == 3 then
        netModeStr = "3G TD网络"
    elseif netMode == 4 then
        netModeStr = "4G LTE网络"
    elseif netMode == 5 then
        netModeStr = "3G WCDMA网络"
    end
    selfCheckContent = "[" .. sysTime .. "]" .. "mqtt连接状态：" ..
                           mqttConnectStatStr .. ",网络注册状态：" ..
                           netConnectStatStr .. ",信号强度:" .. netRssiStr ..
                           ",注册的网络类型:" .. netModeStr .. "\n"
    logGenerator.generateLogTask(selfCheckContent, "log")

end
-- 计算当前时间
local function getCurrentTime() return os.date("*t") end

-- 计算指定时间的时间戳
local function getTimeStamp(hour, min, second)
    local currentTime = getCurrentTime()
    currentTime.hour = hour
    currentTime.min = min
    currentTime.sec = second
    return os.time(currentTime)
end

-- 计算当天0点的时间戳
local function getMidnightTimeStamp() return getTimeStamp(0, 0, 0) end
function SelfCheckTimed()
    -- 进入循环，不断检查是否到了指定时间
    local currentTime = os.time()
    local midnightTime = getMidnightTimeStamp()
    if currentTime == midnightTime then
        -- 已过了指定时间，执行任务
        timedSelfCheckTaske()
    else
        if currentTime > midnightTime then
            if (currentTime - midnightTime) <= 10 then
                if countSelfCheck == 0 then
                    timedSelfCheckTaske()
                    countSelfCheck = 1
                end
            else
                if countSelfCheck == 1 then countSelfCheck = 0 end
            end

        end
    end
end
sys.timerLoopStart(SelfCheckTimed, 1000)
